$(function() {

    $('#errors-div').delay(1500).fadeOut(function() {
        $(this).remove();
    });	

    $('#kw').val($.trim($('#kw').val()));

    $(':text').blur(function() {
        $(this).val($.trim($(this).val()));
    });

    // 内容删除 
    $('.delete-item').on('click', function(e) {
        var url = location.pathname + '/' + $(this).data('id');
        var dialog = $('#dialog-delete');
        dialog.find('.btn-modal-ok-delete').not(".disabled").attr('href', url);
        
        dialog.modal('show');
        return false;

    });

    // 图片删除
    $('body').on('click', '.attachments .thumbnail-remove', function(e) {
        e.preventDefault();
        $(this).parent('.thumbnail').remove();
    });
});

// 导出Excel
$('#toExcel').on('click', function() {

    var url = location.href + '&export=1';

    if (!location.search) {
        url = location.href + '?export=1';
    }
    
    location.href = url;
});

// 表单JS验证
if ($.validator) {
    $('form').validator({
        bindClassTo: ".form-group",
        msgClass: "n-bottom-msg",
        timely: 3,
        target: function(elem){
            var $formitem = $(elem).closest('.form-group'),
                $msgbox = $formitem.find('span.msg-box');
            if (!$msgbox.length) {
                $msgbox = $('<span class="msg-box validator-msg"></span>').appendTo($formitem.find('.row'));
            }
            return $msgbox;
        }    
    });
}

// 导航栏凸显
(function() {
    var currentTextArray = [];
    $('.breadcrumb > li').each(function() {
        currentTextArray.push($(this).text());
    });
    currentTextArray.reverse();

    $.each(currentTextArray, function(index, item) {
        if ($(".top-nav > li[text='"+item+"']").length > 0) {
            $(".top-nav > li[text='"+item+"']").eq(0).addClass('active').siblings().removeClass('active');
            return false;
        }
    });
}());

swal.setDefaults({ confirmButtonText: '确定', cancelButtonText: '取消', allowOutsideClick: true });

// 分页栏悬浮
function windowSizeAdjust()
{
    if ($('.table-footer').length > 0) {
        var footerHeight = $('.table-footer').outerHeight();
        var width = $('.table-list').outerWidth();

        $('.table-list').css('margin-bottom', footerHeight-1);

        if ($(document).outerHeight() > $(window).outerHeight()) {
            $('.table-fixed').css({position: "fixed", margin: "0px", width: width+'px',  bottom: "0px"});
        } else {
            $('.table-fixed').css({position: 'relative', "margin": "-"+ footerHeight +"px 0px 0px", width: '100%', bottom: "auto"});
        }
    }
}

windowSizeAdjust();

$(window).resize(function() {
    windowSizeAdjust();
});

$(function() {
    if ($('#sweet-alert').length > 0) {
        swal("", $('#sweet-alert').html(), $('#sweet-alert').data('type'));
    }
});

function changeQueryParameter(key, value)
{
    var queryParameters = {}, queryString = location.search.substring(1), re = /([^&=]+)=([^&]*)/g, m;
    while (m = re.exec(queryString)) {
        queryParameters[decodeURIComponent(m[1])] = decodeURIComponent(m[2]);
    }

    if (typeof key == 'object') {
        $.each(key, function(i, n) {
            queryParameters[i] = n;
        });
    } else {
        if (value == '') {
            delete queryParameters[key];
        } else {
            queryParameters[key] = value;
        }
    }
    // console.log(queryParameters);
    location.search = $.param(queryParameters); 
}

// 合同号大写
$('body').on('keypress', "input[name='contract_no']", function(e) {
   e.preventDefault();
    $(this).val($(this).val() + String.fromCharCode(event.which).toUpperCase());
});

$('body').on('click','.pretty-photo-blank', function(e) {
    open($('.pp_gallery ul li.selected img').attr('src'));
});

$('.tab-view a').click(function (e) {
    e.preventDefault()
    $(this).tab('show')
})



